home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / StdLib / system.c < prev    next >
C/C++ Source or Header  |  1990-04-19  |  5KB  |  228 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /*    system.c
  18.  *
  19.  *    system() - Executes a program within the context of the current process
  20.  *    _PathAccess() - Attempts access of file along a trial path
  21.  *
  22.  *    Note: The following two routines are a non-optimal solution to the
  23.  *          need to find the full path name given only a directory lock.
  24.  *
  25.  *    _SearchPath() - Attempts to locate a file along the current CLI path
  26.  */
  27.  
  28. #include <exec/types.h>
  29. #include <exec/memory.h>
  30. #include <exec/alerts.h>
  31. #include <libraries/dosextens.h>
  32. #include <ctype.h>
  33.  
  34. #define MAXPATH (1024L)
  35.  
  36. extern long LoadSeg();
  37. extern void UnLoadSeg();
  38.  
  39. char *_SearchPath();
  40. char *_PathAccess();
  41.  
  42. int system(line)
  43. char *line;
  44. {
  45.     char *buffer;
  46.     char *command;
  47.     char *args;
  48.     char *ptr;
  49.     char scan_for;
  50.     long segment_list;
  51.     long return_code;
  52.  
  53.     buffer = malloc(strlen(line)+1+3+1); /* null + space to LW align + LF */
  54.     if (!(ptr = buffer)) {
  55.         Alert(AG_NoMemory, 0L);
  56.         return(-1);
  57.     }
  58.  
  59.     while (isspace(*line))
  60.         line++;
  61.  
  62.     if (*line == '"')    {
  63.         scan_for = '"';
  64.         line++;
  65.     }
  66.     else
  67.         scan_for = ' ';
  68.  
  69.     while (*ptr = *line)    {
  70.         if ((isspace(*line) && (scan_for == ' ')) || (*line == scan_for))
  71.             break;
  72.         else    {
  73.             ptr++;
  74.             line++;
  75.         }
  76.     }
  77.  
  78.     *ptr++ = '\0';
  79.     args = ptr = (ptr+3) & 0xfffffffc;  /* Longword align */
  80.  
  81.     while (isspace(*line))
  82.         line++;
  83.     while (*ptr = *line)
  84.         ptr++, line++;
  85.     *ptr++ = '\n';
  86.     *ptr++ = '\0';
  87.  
  88.     command = _SearchPath(buffer);
  89.     if (!command) {
  90.         free(buffer);
  91.         return(-1);
  92.     }
  93.  
  94.     segment_list = LoadSeg(command);
  95.     if (!segment_list) {
  96.         free(buffer);
  97.         free(command);
  98.         return(-1);
  99.     }
  100.  
  101.     return_code = System0(command, segment_list, args);
  102.  
  103.     UnLoadSeg(segment_list);
  104.     free(buffer);
  105.     free(command);
  106.     return(return_code);
  107. }
  108.  
  109. char *_SearchPath(target)
  110. char *target;
  111. {
  112.     struct CommandLineInterface *this_cli;
  113.     struct FileLock *pl;
  114.     struct FileLock *nextpl;
  115.     struct FileInfoBlock *fib;
  116.     char *fullpath = 0L;
  117.     char *buffer;
  118.     struct {
  119.         BPTR NextPath;
  120.         BPTR PathLock;
  121.     } *p_chain;
  122.     unsigned int count;
  123.  
  124. /* Check current directory:
  125.  */
  126.     fullpath = _PathAccess("", target);
  127.     if (fullpath)
  128.         return(fullpath);
  129.  
  130. /* Then check search path:
  131.  */
  132.     this_cli = BADDR(((struct Process *)FindTask(0L))->pr_CLI);
  133.     if (this_cli == 0L) {
  134.         return(0L);
  135.     }
  136.  
  137.     p_chain = BADDR(this_cli->cli_CommandDir);
  138.  
  139.     buffer = malloc(MAXPATH+2); /* Path + nul + initial slosh byte */
  140.     if (buffer == 0L) {
  141.         Alert(AG_NoMemory, 0L);
  142.         return(0L);
  143.     }
  144.  
  145.  
  146.     fib = malloc(sizeof(struct FileInfoBlock));
  147.     if (!fib) {
  148.         Alert(AG_NoMemory, 0L);
  149.         return(0L);
  150.     }
  151.  
  152.     while (p_chain)    {
  153.         pl = DupLock(p_chain->PathLock);
  154.  
  155.         buffer[MAXPATH+1] = '\0';
  156.         fullpath = &buffer[MAXPATH];
  157.  
  158.         while (pl) {
  159.             if (Examine(pl, fib)) {
  160.                 if (nextpl = ParentDir(pl))
  161.                     *--fullpath = '/';
  162.                 else
  163.                     *--fullpath = ':';
  164.                 fullpath -= (count = strlen(fib->fib_FileName));
  165.                 if (fullpath <= buffer) {
  166.                     UnLock(pl);
  167.                     if (nextpl)
  168.                         UnLock(nextpl);
  169.                     goto next_path;
  170.                 }
  171.                 bcopy( fib->fib_FileName, fullpath, count );
  172.                 UnLock(pl);
  173.                 pl = nextpl;
  174.             }
  175.             else {
  176.                 UnLock(pl);
  177.                 goto next_path;
  178.             }
  179.         }
  180.  
  181.         fullpath = _PathAccess(fullpath, target);
  182.         if (fullpath) {
  183.             UnLock(nextpl);
  184.             free(buffer);
  185.             free(fib);
  186.             return(fullpath);
  187.         }
  188.  
  189. next_path:
  190.         p_chain = BADDR(p_chain->NextPath);
  191.     }
  192.  
  193. /* If that fails, default to c:
  194.  */
  195.     fullpath = _PathAccess("c:", target);
  196.  
  197.     free(buffer);
  198.     free(fib);
  199.     return(fullpath);  /* Gets 0L from _PathAccess if failed */
  200.  
  201. }
  202.  
  203.  
  204. char *_PathAccess(path, target)
  205. char *path;
  206. char *target;
  207. {
  208.     char *buf;
  209.     struct FileLock *fl;
  210.  
  211.     buf = malloc(1024);
  212.     if (buf == 0L) {
  213.         Alert(AG_NoMemory, 0L);
  214.         return(NULL);
  215.     }
  216.  
  217.     buf[0] = '\0';
  218.     strcat(buf, path);
  219.     strcat(buf, target);
  220.     if (fl = Lock(buf, ACCESS_READ)) {
  221.         UnLock(fl);
  222.         return(buf);
  223.     }
  224.  
  225.     free(buf);
  226.     return(0L);
  227. }
  228.